package com.yiqi.lottery.feature.login.view;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import butterknife.BindView;
import butterknife.OnClick;
import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.PowerfulEditText;
import com.yiqi.lottery.R;
import com.yiqi.lottery.feature.login.contract.LoginContract;
import com.yiqi.lottery.feature.login.dependencies.login.DaggerLoginComponent;
import com.yiqi.lottery.feature.login.dependencies.login.LoginPresenterModule;

import javax.inject.Inject;

public class LoginActivity extends BaseActivity implements LoginContract.View, CompoundButton.OnCheckedChangeListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @Inject
    LoginContract.Presenter mPresenter;
    @BindView(R.id.edt_user_login_mobile)
    PowerfulEditText mEdtUserLoginMobile;
    @BindView(R.id.edt_user_login_password)
    PowerfulEditText mEdtUserLoginPassword;
    @BindView(R.id.cbx_user_login_remember)
    CheckBox mCbxUserLoginRemember;



    private boolean isRemember = false;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_login;
    }

    @Override
    protected void initInjector() {
        DaggerLoginComponent.builder().loginPresenterModule(new LoginPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "登录", true, true);

    }

    @Override
    protected void initData() {
        mPresenter.initUserAccount();
    }

    @Override
    protected void initEvent() {
        mCbxUserLoginRemember.setOnCheckedChangeListener(this);
    }

    @OnClick({R.id.btn_user_login, R.id.btn_user_forget, R.id.btn_user_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_user_login:
                mPresenter.doUserLogin(mEdtUserLoginMobile.getText().toString(), mEdtUserLoginPassword.getText().toString(), isRemember);
                break;
            case R.id.btn_user_forget:
                startActivity(new Intent(this, ResetPwdActivity.class));
                break;
            case R.id.btn_user_register:
                startActivity(new Intent(this, RegisterActivity.class));
                finish();
                break;
        }
    }

    @Override
    public void showLoginSuccess() {
      finish();
    }

    @Override
    public void showLogOutSuccess() {

    }

    @Override
    public void showLoginError(String error) {
        T.showShort(error);
    }

    @Override
    public void setUserAccount(String username, String password, boolean isRemember) {
        mEdtUserLoginMobile.setText(username);
        mEdtUserLoginPassword.setText(password);
        mCbxUserLoginRemember.setChecked(isRemember);
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        isRemember = isChecked;
        mPresenter.setRememberPassword(isRemember);
    }
}
